import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index.js'
import Home from '../views/Home.vue'
import { Message } from 'view-design'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children: [
      {
        path: '',
        name: 'Operation',
        component: () =>
          import(/* webpackChunkName: "operation" */ '../views/Operation.vue'),
        meta: {
          title: '首页'
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () =>
      import(/* webpackChunkName: "login" */ '../views/Login.vue'),
    meta: {
      title: '登录'
    }
  },
  {
    path: '/regester',
    name: 'Regester',
    component: () =>
      import(/* webpackChunkName: "regester" */ '../views/Regester.vue'),
    meta: {
      title: '注册'
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  let needLogin =
    to.path == '/login' ? true : to.path == '/regester' ? true : false
  if (!needLogin) {
    let isLogin = window.sessionStorage.getItem('isLogin')
    if (isLogin) {
      next()
    } else {
      Message.error('您未登录！请先登录！')
      router.push('/login')
    }
  } else {
    next()
  }
})

export default router
